package com.atguigu.gmallpublisher.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmallpublisher.beans.DAUHour;
import com.atguigu.gmallpublisher.beans.GMVHour;
import com.atguigu.gmallpublisher.service.PublisherServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

@RestController
public class PublisherController {
    @Autowired
    PublisherServiceImp publisherServiceImp;
    //日活总数 http://localhost:8070/realtime-total?date=2021-05-29
    @RequestMapping(value = "/realtime-total")
    public Object handle1(String date){
        Integer dauByDate = publisherServiceImp.getDauByDate(date);
        Integer newMidCount = publisherServiceImp.getNewMidCountByDate(date);
        Double totalAmountByDate = publisherServiceImp.getTotalAmountByDate(date);
        //要返回的是一个数组,数组里是两个json字符串
            //使用jackson,不用fastjson,fastjson有bug,返回页面解析出的json可能有转义\出现
            //创建数组容器,也可创建jsonArray
        JSONArray jsonArray = new JSONArray();

        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
            //当日日活json对象
        jsonObject.put("id","dau");
        jsonObject.put("name","当日日活");
        jsonObject.put("value",dauByDate);
            //新增设备json对象
        jsonObject1.put("id","new_mid");
        jsonObject1.put("name","新增设备");
        jsonObject1.put("value",newMidCount);
            //新增交易额
        jsonObject2.put("id","order_amount");
        jsonObject2.put("name","新增交易额");
        jsonObject2.put("value",totalAmountByDate);
            //将两个对象加入容器
        jsonArray.add(jsonObject);
        jsonArray.add(jsonObject1);
        jsonArray.add(jsonObject2);

        //Springboot判断是否是字面量,如果不是,利用Jackson转为jsonstr
        return jsonArray;
        /*  [{"id":"dau","name":"当日日活","value":1200},
            {"id":"new_mid","name":"新增设备","value":233}]*/
    }

    //分时统计 http://localhost:8070/realtime-hours?id=dau&date=2020-08-15
    @RequestMapping(value = "/realtime-hours")
    public Object handle2(String id,String date){

        JSONObject jsonObject = new JSONObject();

        LocalDate today = LocalDate.parse(date);
        LocalDate yesterday = today.minusDays(1);

        if(id.equals("dau")){
            List<DAUHour> hourDAUNum = publisherServiceImp.getHourDAUNum(today.toString());
            List<DAUHour> last = publisherServiceImp.getHourDAUNum((yesterday.toString()));
            JSONObject jsonObject1 = getJsonObject(hourDAUNum);
            JSONObject jsonObject2 = getJsonObject(last);
            jsonObject.put("yesterday",jsonObject2);
            jsonObject.put("today",jsonObject1);
        }else{
            List<GMVHour> hourOrderAmount = publisherServiceImp.getHourOrderAmount(today.toString());
            List<GMVHour> hourOrderAmountYesterday = publisherServiceImp.getHourOrderAmount(yesterday.toString());
            JSONObject jsonObject1 = getJsonObject1(hourOrderAmount);
            JSONObject jsonObject2 = getJsonObject1(hourOrderAmountYesterday);
            jsonObject.put("yesterday",jsonObject2);
            jsonObject.put("today",jsonObject1);
        }

        return jsonObject;
        /*{"yesterday":{"11":383,"12":123,"17":88,"19":200 },
            "today":{"12":38,"13":1233,"17":123,"19":688 }}
            因为要查询每小时的日活数,需要获取两个字段,将其封装成beans
            */
    }

    public JSONObject getJsonObject(List<DAUHour> hourDAUNum){
        JSONObject jsonObject = new JSONObject();
        for (DAUHour dauHour : hourDAUNum) {
            jsonObject.put(dauHour.getHour(),dauHour.getNum());
        }
        return jsonObject;
    }

    public JSONObject getJsonObject1(List<GMVHour> hourOrderAmount){
        JSONObject jsonObject = new JSONObject();
        for (GMVHour gmvHour : hourOrderAmount) {
            jsonObject.put(gmvHour.getHour(),gmvHour.getAmount());
        }
        return jsonObject;
    }

    /*http://localhost:8070/sale_detail?date=2020-08-21&startpage=1&size=5&keyword=小米手机*/

    @RequestMapping(value = "/sale_detail")
    public JSONObject handle3(String date,Integer startpage,Integer size,String keyword) throws IOException {
        JSONObject orderDetail = publisherServiceImp.getOrderDetail(date, startpage, size, keyword);
        return orderDetail;
    }
}
